package com.gtl.meishi.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.gtl.meishi.R;
import com.gtl.meishi.bean.User;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LoginActivity extends AppCompatActivity {

    private EditText mEditTextName, mEditTextPwd;
    public static String APPID = "4385626f2be1bdde82da2e6ad2cebc0e";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEditTextName= (EditText) findViewById(R.id.edittext_name_login);
        mEditTextPwd= (EditText) findViewById(R.id.edittext_pwd_login);
        Bmob.initialize(this, APPID);	//

    }

    public  void  doClick(View view){

        switch (view.getId()){
            case R.id.button_send_login:
                String name=mEditTextName.getText().toString();
                String pwd=mEditTextPwd.getText().toString();
                if(name.length()>0&&pwd.length()>0){
                    login(name,pwd);
                }



            break;
        }

    }

    public void  login (String name,String pwd){
        //查找Person表里面id为6b6c11c537的数据
        BmobQuery<User> bmobQuery = new BmobQuery<User>();
        bmobQuery.addWhereEqualTo("name",name);
        bmobQuery.addWhereEqualTo("password",pwd);
        bmobQuery.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if(e==null){
                    if(list.size()>0){
                        toast("登录成功");
                        finish();
                    }else{
                        toast("登录失败");
                    }

                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }

            }
        });

    }

    public  void  toast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();

    }

}
